package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.Constant;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.GoodsPic;
import com.ruoyi.system.domain.GoodsTags;
import com.ruoyi.system.domain.Tag;
import com.ruoyi.system.mapper.GoodsPicMapper;
import com.ruoyi.system.mapper.GoodsTagsMapper;
import com.ruoyi.system.mapper.TagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GoodsMapper;
import com.ruoyi.system.domain.Goods;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import  com.ruoyi.system.service.IGoodsService;
/**
 * 商品信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@Service
public class GoodsServiceImpl implements IGoodsService {
    @Resource
    private GoodsPicMapper goodsPicMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private GoodsTagsMapper goodsTagsMapper;

    @Resource
    private TagMapper tagMapper;

    /**
     * 查询商品信息
     *
     * @param spuId 商品信息主键
     * @return 商品信息
     */
    @Override
    public Goods selectGoodsBySpuId(Long spuId) {
        Goods goods = goodsMapper.selectGoodsBySpuId(spuId);
        if (goods != null) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setSpuId(spuId);
            List<GoodsPic> goodsPicList = goodsPicMapper.selectGoodsPicList(goodsPic);
            String primaryDescImage = "";
            String primaryDetailImage = "";
            for (int p = 0; p < goodsPicList.size(); p++) {
                GoodsPic gpt = goodsPicList.get(p);
                String path = gpt.getImagePath() + Constant.DEFAULT_SPLIT;
                switch (gpt.getImageType()) {
                    case "0":
                        primaryDescImage += path;
                        break;
                    case "1":
                        primaryDetailImage += path;
                        break;
                }
            }

            if (primaryDescImage.endsWith(Constant.DEFAULT_SPLIT)) {
                primaryDescImage = primaryDescImage.substring(0, primaryDescImage.length() - 1);
            }
            if (primaryDetailImage.endsWith(Constant.DEFAULT_SPLIT)) {
                primaryDetailImage = primaryDetailImage.substring(0, primaryDetailImage.length() - 1);
            }
            goods.setPrimaryDetailImage(primaryDetailImage);
            goods.setPrimaryDescImage(primaryDescImage);

            List<Object> goodsTagsObj = new ArrayList<>();
            List<String> goodsTagsText = new ArrayList<>();
            GoodsTags goodsTags = new GoodsTags();
            goodsTags.setSpuId(spuId);
            List<GoodsTags> goodsTagsList = goodsTagsMapper.selectGoodsTagsList(goodsTags);
            if (goodsTagsList != null && goodsTagsList.size() > 0) {
                for (int td = 0; td < goodsTagsList.size(); td++) {
                    GoodsTags gd = goodsTagsList.get(td);
                    goodsTagsObj.add(gd.getTagId());
                    goodsTagsText.add(gd.getTagTxt());

                }
            }
            goods.setTags(goodsTagsText);
            goods.setGoodsTags(goodsTagsObj);
        }
        return goods;
    }

    /**
     * 查询商品信息列表
     *
     * @param goods 商品信息
     * @return 商品信息
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods) {
        List<Goods> goodsList = goodsMapper.selectGoodsList(goods);
        if (goodsList != null) {
            GoodsPic goodsPic = new GoodsPic();
            for (int i = 0; i < goodsList.size(); i++) {
                Goods tmp = goodsList.get(i);
                goodsPic.setSpuId(tmp.getSpuId());
                List<GoodsPic> goodsPicList = goodsPicMapper.selectGoodsPicList(goodsPic);
                // primaryDescImage:[],
                //        primaryDetailImage:[],
                String primaryDescImage = "";
                String primaryDetailImage = "";
                for (int p = 0; p < goodsPicList.size(); p++) {
                    GoodsPic gpt = goodsPicList.get(p);
                    String path = gpt.getImagePath() + Constant.DEFAULT_SPLIT;
                    switch (gpt.getImageType()) {
                        case "0":
                            primaryDescImage += path;
                            break;
                        case "1":
                            primaryDetailImage += path;
                            break;
                    }
                }
                if (primaryDescImage.endsWith(Constant.DEFAULT_SPLIT)) {
                    primaryDescImage = primaryDescImage.substring(0, primaryDescImage.length() - 1);
                }
                if (primaryDetailImage.endsWith(Constant.DEFAULT_SPLIT)) {
                    primaryDetailImage = primaryDetailImage.substring(0, primaryDetailImage.length() - 1);
                }
                tmp.setPrimaryDetailImage(primaryDetailImage);
                tmp.setPrimaryDescImage(primaryDescImage);


                List<Object> goodsTagsObj = new ArrayList<>();
                List<String> goodsTagsText = new ArrayList<>();
                GoodsTags goodsTags = new GoodsTags();
                goodsTags.setSpuId(tmp.getSpuId());
                List<GoodsTags> goodsTagsList = goodsTagsMapper.selectGoodsTagsList(goodsTags);
                if (goodsTagsList != null && goodsTagsList.size() > 0) {
                    for (int td = 0; td < goodsTagsList.size(); td++) {
                        GoodsTags gd = goodsTagsList.get(td);
                        goodsTagsObj.add(gd.getTagId());
                        goodsTagsText.add(gd.getTagTxt());

                    }
                }
                tmp.setTags(goodsTagsText);
                tmp.setGoodsTags(goodsTagsObj);
            }

        }
        return goodsList;
    }

    /**
     * 新增商品信息
     *
     * @param goods 商品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGoods(Goods goods) {
        goods.setCreateTime(DateUtils.getNowDate());

        goodsMapper.insertGoods(goods);

        if (goods.getGoodsTags() != null) {
            for (int i = 0; i < goods.getGoodsTags().size(); i++) {
                Object tag = goods.getGoodsTags().get(i);
                Long tagId = null;
                if (tag instanceof String) {
                    //添加
                    Tag tag1 = new Tag(tag.toString());
                    tagMapper.insertTag(tag1);
                    tagId = tag1.getTagId();
                } else if (tag instanceof Integer) {
                    //id
                    tagId = Long.parseLong(tag.toString());
                }
                if (tagId != null) {
                    goodsTagsMapper.insertGoodsTags(new GoodsTags(goods.getSpuId(), tagId));
                }
            }
        }
        String[] primaryDescImage = goods.getPrimaryDescImage().split(Constant.DEFAULT_SPLIT);
        String[] primaryDetailImage = goods.getPrimaryDetailImage().split(Constant.DEFAULT_SPLIT);

        List<GoodsPic> goodsPics = new ArrayList<>();
        if (primaryDescImage != null) {
            String img = null;
            for (int i = 0; i < primaryDescImage.length; i++) {
                img = primaryDescImage[i];
                goodsPics.add(new GoodsPic(goods.getSpuId(), "0", img));
            }
        }
        if (primaryDetailImage != null) {
            String img = null;
            for (int i = 0; i < primaryDetailImage.length; i++) {
                img = primaryDetailImage[i];
                goodsPics.add(new GoodsPic(goods.getSpuId(), "1", img));
            }
        }
        for (int i = 0; i < goodsPics.size(); i++) {
            goodsPicMapper.insertGoodsPic(goodsPics.get(i));
        }

        return 1;
    }

    /**
     * 修改商品信息
     *
     * @param goods 商品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGoods(Goods goods) {
        goods.setUpdateTime(DateUtils.getNowDate());
        Goods dbData = goodsMapper.selectGoodsBySpuId(goods.getSpuId());
        if(dbData==null)
            return 0;

        goodsMapper.updateGoods(goods);

        goodsTagsMapper.deleteGoodsTagsBySpuId(goods.getSpuId());
        goodsPicMapper.deleteGoodsPicBySpuId(goods.getSpuId());
        if (goods.getGoodsTags() != null) {
            for (int i = 0; i < goods.getGoodsTags().size(); i++) {
                Object tag = goods.getGoodsTags().get(i);
                Long tagId = null;
                if (tag instanceof String) {
                    //添加
                    Tag tag1 = new Tag(tag.toString());
                    tagMapper.insertTag(tag1);
                    tagId = tag1.getTagId();
                } else if (tag instanceof Integer) {
                    //id
                    tagId = Long.parseLong(tag.toString());
                }
                if (tagId != null) {
                    goodsTagsMapper.insertGoodsTags(new GoodsTags(goods.getSpuId(), tagId));
                }
            }
        }
        String[] primaryDescImage = goods.getPrimaryDescImage().split(Constant.DEFAULT_SPLIT);
        String[] primaryDetailImage = goods.getPrimaryDetailImage().split(Constant.DEFAULT_SPLIT);

        List<GoodsPic> goodsPics = new ArrayList<>();
        if (primaryDescImage != null) {
            String img = null;
            for (int i = 0; i < primaryDescImage.length; i++) {
                img = primaryDescImage[i];
                goodsPics.add(new GoodsPic(goods.getSpuId(), "0", img));
            }
        }
        if (primaryDetailImage != null) {
            String img = null;
            for (int i = 0; i < primaryDetailImage.length; i++) {
                img = primaryDetailImage[i];
                goodsPics.add(new GoodsPic(goods.getSpuId(), "1", img));
            }
        }
        for (int i = 0; i < goodsPics.size(); i++) {
            goodsPicMapper.insertGoodsPic(goodsPics.get(i));
        }

        return 1;
    }

    /**
     * 批量删除商品信息
     *
     * @param spuIds 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsBySpuIds(Long[] spuIds) {
        return goodsMapper.deleteGoodsBySpuIds(spuIds);
    }

    /**
     * 删除商品信息信息
     *
     * @param spuId 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsBySpuId(Long spuId) {
        return goodsMapper.deleteGoodsBySpuId(spuId);
    }
}
